package com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.service.ILvRukumingxiService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.domain.FinanceBuyInvoiceStoreRel;
import com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.service.IFinanceBuyInvoiceStoreRelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购发票和入库明细关系Controller
 * @author lmm
 * @date 2025-07-25
 */
@RestController
@RequestMapping("/financeBuyInvoiceStoreRel/financeBuyInvoiceStoreRel")
public class FinanceBuyInvoiceStoreRelController extends BaseController {
    @Autowired
    private IFinanceBuyInvoiceStoreRelService financeBuyInvoiceStoreRelService;
    @Autowired
    private ILvRukumingxiService lvRukumingxiService;



    /**
     * 查询采购发票和入库明细关系列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoiceStoreRel:query')")
    @GetMapping("/list")
    public TableDataInfo list(FinanceBuyInvoiceStoreRel entity) {
        startPage();
        List<FinanceBuyInvoiceStoreRel> list = financeBuyInvoiceStoreRelService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出采购发票和入库明细关系列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoiceStoreRel:export')")
    @Log(title = "采购发票和入库明细关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinanceBuyInvoiceStoreRel entity) {
        List<FinanceBuyInvoiceStoreRel> list = financeBuyInvoiceStoreRelService.selectList(entity);
        ExcelUtil<FinanceBuyInvoiceStoreRel> util = new ExcelUtil<FinanceBuyInvoiceStoreRel>(FinanceBuyInvoiceStoreRel. class);
        util.exportExcel(response, list, "采购发票和入库明细关系数据");
    }

    /**
     * 获取采购发票和入库明细关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoiceStoreRel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(financeBuyInvoiceStoreRelService.selectById(id));
    }

    /**
     * 新增采购发票和入库明细关系
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoiceStoreRel:add')")
    @Log(title = "采购发票和入库明细关系", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FinanceBuyInvoiceStoreRel entity) {
        return toAjax(financeBuyInvoiceStoreRelService.insert(entity));
    }

    /**
     * 修改采购发票和入库明细关系
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoiceStoreRel:edit')")
    @Log(title = "采购发票和入库明细关系", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody FinanceBuyInvoiceStoreRel entity) {
        return toAjax(financeBuyInvoiceStoreRelService.update(entity));
    }

    /**
     * 删除采购发票和入库明细关系
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoiceStoreRel:remove')")
    @Log(title = "采购发票和入库明细关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {

        return toAjax(financeBuyInvoiceStoreRelService.deleteByIds(ids));
    }



    /**
     * 新增采购发票和入库明细关系
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoiceStoreRel:add')")
    @Log(title = "采购发票和入库明细关系", businessType = BusinessType.INSERT)
    @PostMapping("/addRukumingxiList")
    public AjaxResult addRukumingxiList(@RequestBody FinanceBuyInvoiceStoreRel entity) {
        List<LvRukumingxi> rukumingxiList = entity.getRukumingxiList();
        for (LvRukumingxi lvRukumingxi : rukumingxiList) {
            // 先查询是否有
            FinanceBuyInvoiceStoreRel rel =
                    financeBuyInvoiceStoreRelService.selectNotCommit(entity.getInvoiceId(),lvRukumingxi.getId(),
                            lvRukumingxi.getGoodsId(),SecurityUtils.getUsername(),BusinessConstants.STATUS_NO);
            if(rel == null){
                FinanceBuyInvoiceStoreRel financeBuyInvoiceStoreRel = new FinanceBuyInvoiceStoreRel();
                financeBuyInvoiceStoreRel.setGoodsCode(lvRukumingxi.getCode());
                financeBuyInvoiceStoreRel.setGoodsId(lvRukumingxi.getGoodsId());
                if(StringUtils.isBlank(lvRukumingxi.getGoodsModel())){
                    financeBuyInvoiceStoreRel.setGoodsModel(lvRukumingxi.getParams().get("goodsModel")+"");
                }
                if(StringUtils.isBlank(lvRukumingxi.getGoodsName())){
                    financeBuyInvoiceStoreRel.setGoodsName(lvRukumingxi.getParams().get("goodsName")+"");
                }
                financeBuyInvoiceStoreRel.setStoreDetailId(lvRukumingxi.getId());
                financeBuyInvoiceStoreRel.setStoreNos(lvRukumingxi.getRukuno());
                financeBuyInvoiceStoreRel.setStoreId(lvRukumingxi.getParentId());
                financeBuyInvoiceStoreRel.setAmount(lvRukumingxi.getInvoiceTemp());
                financeBuyInvoiceStoreRelService.insert(financeBuyInvoiceStoreRel);
            }else{
                rel.setAmount(rel.getAmount().add(lvRukumingxi.getInvoiceTemp()));
                financeBuyInvoiceStoreRelService.update(rel);
            }
            lvRukumingxi.setInvoiceRemain(lvRukumingxi.getInvoiceRemain().subtract(lvRukumingxi.getInvoiceTemp()));
            lvRukumingxi.setInvoiceAmount(lvRukumingxi.getInvoiceAmount().add(lvRukumingxi.getInvoiceTemp()));
            if(lvRukumingxi.getInvoiceRemain().compareTo(BigDecimal.ZERO) == 0){
                lvRukumingxi.setInvoiceStatus(BusinessConstants.TAX_STATUS_2);
            }else if (lvRukumingxi.getInvoiceRemain().compareTo(BigDecimal.ZERO) > 0){
                lvRukumingxi.setInvoiceStatus(BusinessConstants.TAX_STATUS_1);
            }
            lvRukumingxiService.update(lvRukumingxi);
        }
        return toAjax(rukumingxiList.size());
    }


}
